package com.yingpai.fitness.dvp_2_0.main.activity.mine.address;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.address.AddAddressBean;
import com.yingpai.fitness.entity.address.AddressBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/11/22.
 */

public class AddressSelectActivity extends CommonActivity {
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.listview)
    ListView listview;
    private QuickAdapter<AddressBean.MapBean.AddressesBean> quickAdapter;
    private TokenBean tokenBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_activity_select_address);

    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HttpParams httpParams = new HttpParams();
        httpParams.put("customerId", tokenBean.getId());
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.RECEIVER_ADDRESS, httpParams, Url.RECEIVER_ADDRESS, new Callback<AddressBean>() {
            @Override
            public void onSuccess(AddressBean addressBean) {
                if (addressBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    if (addressBean.getMap().getAddresses() != null && addressBean.getMap().getAddresses().size() != 0) {
                        empty.setVisibility(View.GONE);
                        quickAdapter.clear();
                        quickAdapter.addAll(addressBean.getMap().getAddresses());
                        quickAdapter.notifyDataSetChanged();
                    } else {
                        empty.setVisibility(View.VISIBLE);
                    }
                } else {
                    empty.setVisibility(View.VISIBLE);
                    showToast(addressBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                empty.setVisibility(View.VISIBLE);
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({R.id.right_tv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.right_tv:
                startActivity(AddAddressActivity.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initView() {
        setTitle("选择地址");
        Drawable drawableLeft = getResources().getDrawable(
                R.mipmap.add_address);
        rightTv.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                null, null, null);
        EventBus.getDefault().register(this);
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);

        quickAdapter = new QuickAdapter<AddressBean.MapBean.AddressesBean>(this, R.layout.item_select_address) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final AddressBean.MapBean.AddressesBean item) {
                helper.setText(R.id.name, item.getReceiverName());
                helper.setText(R.id.phone, item.getReceiverPhoneNo());
                helper.setText(R.id.address, item.getAddress());
                final CheckBox set_default = helper.getView(R.id.set_default);
                if (Constant.REQUEST_SUCCESS_0.equals(item.getStatus())) {
                    set_default.setChecked(false);
                } else {
                    set_default.setChecked(true);
                }

                set_default.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (set_default.isChecked()) {
                            showDialog(context, null);
                            HttpParams httpParams = new HttpParams();
                            httpParams.put("id", item.getId());
                            httpParams.put("customerId", tokenBean.getId());
                            httpParams.put("address", item.getAddress());
                            httpParams.put("receiverName", item.getReceiverName());
                            httpParams.put("receiverPhoneNo", item.getReceiverPhoneNo());
                            httpParams.put("status", 1);
                            finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.UPDATA_ADDRESS, httpParams, Url.UPDATA_ADDRESS, new Callback<AddAddressBean>() {
                                @Override
                                public void onSuccess(AddAddressBean addAddressBean) {
                                    if (Constant.REQUEST_SUCCESS.equals(addAddressBean.getResult())) {
                                        for (int i = 0; i < quickAdapter.getCount(); i++) {
                                            if (i != helper.getPosition()) {
                                                quickAdapter.getItem(i).setStatus(Constant.REQUEST_SUCCESS_0);
                                            } else {
                                                quickAdapter.getItem(i).setStatus("1");
                                                EventBus.getDefault().post(quickAdapter.getItem(i));
                                            }
                                            notifyDataSetChanged();
                                        }
                                    } else {
                                        showToast(addAddressBean.getMsg());
                                    }
                                    dismissDialog();
                                }
                            });
                        }
                    }
                });

                TextView edit = helper.getView(R.id.edit);
                edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("edit_address", item);
                        startActivity(AddAddressActivity.class, bundle);
                    }
                });

                TextView delete = helper.getView(R.id.delete);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HttpParams httpParams = new HttpParams();
                        httpParams.put("id", item.getId());
                        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.DELETE_ADDRESS, httpParams, Url.DELETE_ADDRESS, new Callback<BaseResponse>() {
                            @Override
                            public void onSuccess(BaseResponse baseResponse) {
                                if (Constant.REQUEST_SUCCESS.equals(baseResponse.getResult())) {
                                    quickAdapter.remove(helper.getPosition());
                                    notifyDataSetChanged();
                                } else {
                                    showToast(baseResponse.getMsg());
                                }
                            }
                        });
                    }
                });
            }
        };

        listview.setAdapter(quickAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EventBus.getDefault().post(quickAdapter.getItem(position));
                finishActivity();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void OnMessageEvent(String s) {
        if (Constant.UPDATA_ADDRESS.equals(s)) {
            initData();
        }
    }
}
